﻿using Lk.WebTow.Common.Encrypt;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Lk.WebTow.Data
{


    public class ApplicationDbContextSeed
    {
        private UserManager<NcManager> userManager;

        public async Task SendAsync(ApplicationDbContext context, IServiceProvider service)
        {
            if (!context.NcManager.Any())
            {
                if (!context.NcManagerRoleValue.Any())
                {
                    var role = new NcManagerRole() { IsSys = 1, RoleName = "admin", RoleType = 1 };
                    var roleInfo = await context.AddAsync<NcManagerRole>(role);
                    await context.SaveChangesAsync();
                    var managerRoleValue = new NcManagerRoleValue()
                    {
                        ActionType = "admin",
                        NavName = "admin",
                        Role = role,
                        RoleId = roleInfo.Entity.Id

                    };
                 
                    await context.AddAsync<NcManagerRoleValue>(managerRoleValue);
                    await context.SaveChangesAsync();
                }

                // userManager = service.GetRequiredService<UserManager<NcManager>>();

                var saltValue = "12345";
                var defaultUser = new NcManager()
                {
                    UserName = "test",
                    AddTime = DateTime.Now,
                    IsAudit = 1,
                    Email = "test@qq.com",
                    RealName = "测试",
                    Password = DESEncrypt.Encrypt("123456", saltValue),
                    RoleId = 1,
                    Telephone = "15528193759",
                    Salt = saltValue

                };
                try
                {
                    var result = await context.AddAsync<NcManager>(defaultUser);
                    await context.SaveChangesAsync();
                    if (result == null)
                    {
                        throw new Exception("初始默认用户失败");
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("初始默认用户失败:" + ex.Message);
                }
            }

        }

        public async Task SendAdminAsync(ApplicationDbContext context, IServiceProvider service)
        {
            if (!context.NcManager.Any())
            {

                userManager = service.GetRequiredService<UserManager<NcManager>>();

                var saltValue = "12345";
                var defaultUser = new NcManager()
                {
                    UserName = "test",
                    AddTime = DateTime.Now,
                    IsAudit = 1,
                    Email = "test@qq.com",
                    RealName = "测试",
                    Password = DESEncrypt.Encrypt("123456", saltValue),
                    RoleId = 1,
                    Telephone = "15528193759",
                    Salt = saltValue

                };
                var result = await userManager.CreateAsync(defaultUser);
                if (!result.Succeeded)
                {
                    throw new Exception("初始默认用户失败");
                }
            }

        }
    }
}
